from sklearn import datasets, model_selection, metrics
from sklearn.neighbors import KNeighborsClassifier
from matplotlib import pyplot as plt
import numpy as np


class DigitIdentify(object):
    def show_digits(self):
        digits = datasets.load_digits()
        plt.imshow(digits.images[0], cmap=plt.cm.gray_r)
        plt.show()

    def digit_identify(self):
        digits = datasets.load_digits()
        knn = KNeighborsClassifier()
        knn.fit(digits.data, digits.target)
        test_data = [
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
            0, 0, 8, 14, 8, 1, 0, 0,
        ]
        predict_result = knn.predict(np.array(test_data).reshape(1, -1))
        print(predict_result)

    def get_accuracy_score(self, test_size=0.25):
        X, y = datasets.load_digits(return_X_y=True)
        knn = KNeighborsClassifier()
        X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=test_size)
        knn.fit(X_train, y_train)
        y_pred = knn.predict(X_test)
        score = metrics.accuracy_score(y_test, y_pred)
        print(score)


if __name__ == '__main__':
    di = DigitIdentify()
    # di.show_digits()
    di.get_accuracy_score()
